package com.wondersgroup.easyexceldome.util;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.metadata.BaseRowModel;
import com.alibaba.excel.metadata.Sheet;
import com.alibaba.excel.support.ExcelTypeEnum;
import com.alibaba.excel.write.merge.AbstractMergeStrategy;
import com.wondersgroup.easyexceldome.model.TestDto;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.util.List;

public class ExcelUtil {
    /**已过时!!!
     * 导出 Excel ：一个 sheet，带表头
     * @param response HttpServletResponse
     * @param list 数据 list，每个元素为一个 BaseRowModel
     * @param fileName 导出的文件名
     * @param sheetName 导入文件的 sheet 名
     * @param model 映射实体类，Excel 模型
     */
    public static void write2Excel(HttpServletResponse response, List<? extends BaseRowModel> list,
                                   String fileName, String sheetName, BaseRowModel model)throws Exception  {
        ExcelWriter writer = new ExcelWriter(getOutputStream(fileName, response), ExcelTypeEnum.XLSX);
        Sheet sheet = new Sheet(1, 0, model.getClass());
        sheet.setSheetName(sheetName);
        writer.write(list, sheet);
        writer.finish();
    }

    /**2.*版本更新代码
     * excel的 pojo 实体类取消了 继承BaseRowModel.
     *
     */
    public static void writeExcel(HttpServletResponse response, List<?> list, String fileName, String sheetName)throws Exception  {
        EasyExcel.write(getOutputStream(fileName,response), TestDto.class).sheet(sheetName).doWrite(list);
    }
    /**3.*版本更新代码 合并单元格策略
     * excel的 pojo 实体类取消了 继承BaseRowModel.
     *
     */
    public static void writeExcelMergeStrategy(HttpServletResponse response, List<?> list, String fileName, String sheetName, AbstractMergeStrategy mergeStrategy)throws Exception  {
        EasyExcel.write(getOutputStream(fileName,response), TestDto.class).registerWriteHandler(mergeStrategy)
                //注册合并策略
                .sheet(sheetName).doWrite(list);
    }
    /**
     * 导出文件时为Writer生成OutputStream
     * @param fileName
     * @param response
     * @return
     */
    private static OutputStream getOutputStream(String fileName, HttpServletResponse response) throws Exception {
        try {
            fileName = URLEncoder.encode(fileName, "UTF-8");
            response.setContentType("application/vnd.ms-excel");
            response.setCharacterEncoding("utf8");
            response.setHeader("Content-Disposition", "attachment; filename=" + fileName + ".xlsx");
            response.setHeader("Pragma", "public");
            response.setHeader("Cache-Control", "no-store");
            response.addHeader("Cache-Control", "max-age=0");
            return response.getOutputStream();
        } catch (IOException e) {
            throw new Exception("导出excel表格失败!", e);
        }
    }
}